<script setup>
import {
  Edit, Delete, Position, Upload
} from '@element-plus/icons-vue'

import { ref, computed, watch, watchEffect, getCurrentScope } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
   mintimeservice, getpartpathservice, limittimeservice,
  updatepathservice, mincostservice, deletepathservice, lendbookservice, getallbookcategoryservice
} from '@/api/book.js'


import { BaiduMap, BmlLushu } from 'vue-baidu-map-3x';
import {
  getpathallservice
  , getpathbynameservice,
  setstlnglatservice,
  setedlnglatservice
} from '@/api/allApi.js'





//控制添加分类弹窗
//导入pinia
import { useUserInfoStore } from '@/stores/userInfo.js'
const userInfoStore = useUserInfoStore();


const dialogVisible = ref(false)
const lenddialogVisible = ref(false)
//分页条数据模型
//const pageNum = ref(1)//当前页
const total = ref(20)//总条数
//const pageSize = ref(3)//每页条数
//文章分类数据模型
//定义变量，控制标题
const title = ref('')
//交通路线列表数据模型
const paths = ref([])

//与后端交互的json数据
const pathmodel = ref({
  startPoint: '', //起点
  endPoint: '', //终点
  limitTime: '', //限制时间
  strategy: ''//旅游策略
})

const findmodel = ref({
  pageNum: 1,
  pageSize: 20,
  startPoint: '',
  endPoint: '',
  str: '',
  strategy: ''
})
const pathlist = async () => {
  let result = await getpathallservice();
  console.log(result.data);
  paths.value = result.data;
}
pathlist();

//表单校验


const addpath = async () => {
  console.log(pathmodel.value);
  let result = await addpathservice(pathmodel.value);
  if (result.code == 0) {
    ElMessage.success("添加成功")
    dialogVisible.value = false;
    pathlist();
  }
  else {
    ElMessage.error(result.message ? result.message : '添加失败')
  }
}
const updatepath = async () => {


  ElMessage.success("修改成功")



}
//删除分类
const del = (row) => {
  ElMessageBox.confirm(
    '你确认删除该条交通路线信息吗？',
    '温馨提示',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    }
  )
    .then(async () => {
      //用户点击了确认
      let result = await deletepathservice(row.id)
      ElMessage.success('删除成功')
      //再次调用getAllCategory，获取所有文章分类
      pathlist();
    })
    .catch(() => {
      //用户点击了取消
      ElMessage({
        type: 'info',
        message: '取消删除',
      })
    })
}
//展示编辑弹窗



const lushuPoint = ref({
  start: {
    lng: null,
    lat: null
  },
  end: {
    lng: null,
    lat: null
  },
  pathName: null
});

const entity = ref({
  lng: 106.5478767,
  lat: 29.5647398
})
const istools = ref(false)
const buttonname = ref('设置')
const tools = () => {
  if (buttonname.value == '返回') {
    buttonname.value = '设置'
    istools.value = !istools.value

  }
  else {
    buttonname.value = '返回'
    istools.value = !istools.value
  }
}
const f = ref(false)
const toget = async () => {
  let result = await getpathbynameservice(lushuPoint.value.pathName)
  let path = result.data;
  console.log(lushuPoint.value.pathName);
  if (lushuPoint.value.pathName === '' || lushuPoint.value.pathName === null) {
    ElMessage.error('无法查询，请先选择城镇道路')
  }
  else if (path.stlng === 0 && path.stlat === 0 || path.edlng === 0 && path.edlat === 0) {
    if (userInfoStore.userinfo.userName === 'comum') {
      ElMessage.error('无法查询，请先设置城镇的道路经纬度坐标')
      // tools()
      lushuPoint.value.start.lng = path.stlng
      lushuPoint.value.start.lat = path.stlat
      lushuPoint.value.end.lng = path.edlng
      lushuPoint.value.end.lat = path.edlat
      f.value = false;
      console.log(lushuPoint.value);
    }
    else {
      ElMessage.error('无法查询，请等待或联系管理员设置城镇道路经纬度坐标')
    }
  }
  else if (path.stlng === 0 && path.stlat === 0 && path.edlng === 0 && path.edlat === 0) {
    if (userInfoStore.userinfo.userName === 'comum') {
      ElMessage.error('无法查询，请先设置城镇的道路经纬度坐标')
      // tools()
      lushuPoint.value.start.lng = null
      lushuPoint.value.start.lat = null
      lushuPoint.value.end.lng = null
      lushuPoint.value.end.lat = null
    }
    else {
      ElMessage.error('无法查询，请等待或联系管理员设置城镇道路经纬度坐标')
    }
  }
  else {
    lushuPoint.value.start.lng = path.stlng
    lushuPoint.value.start.lat = path.stlat
    lushuPoint.value.end.lng = path.edlng
    lushuPoint.value.end.lat = path.edlat
    f.value = true
  }
}
const isstart = ref(null);
const setstart = () => {
  if (lushuPoint.value.pathName === '' || lushuPoint.value.pathName === null) {
    ElMessage.error('无法设置，请先选择城镇道路')
  }
  else {
    ElMessageBox.confirm(
      '请在地图上右击鼠标选定起始位置经纬度',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
      .then(async () => {
        ElMessage.success('开始设置')
        isstart.value = true;
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消设置',
        })
        isstart.value = null;
      })
  }
}
const setend = () => {
  if (lushuPoint.value.pathName === '' || lushuPoint.value.pathName === null) {
    ElMessage.error('无法设置，请先选择城镇道路')
  }
  else {
    ElMessageBox.confirm(
      '请在地图上右击鼠标选定终点位置经纬度',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
    )
      .then(async () => {
        ElMessage.success('开始设置')
        isstart.value = false;
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消设置',
        })
        isstart.value = null;
      })
  }
}
const comfir1 = async () => {
  let result = await setstlnglatservice(lushuPoint.value)
  console.log(result);
  if (result.code == 0) {
    ElMessage.success('起始位置经纬度设置成功！')
  }
  else {
    ElMessage.error('起始位置经纬度设置失败！')
  }
  isstart.value = null;
}
const comfir2 = async () => {
  let result = await setedlnglatservice(lushuPoint.value)
  if (result.code == 0) {
    ElMessage.success('终点位置经纬度设置成功！')
  }
  else {
    ElMessage.error('终点位置经纬度设置失败！')
  }
  isstart.value = null;
}
const getCurrentPoint = (e) => {
  console.log(e.point);
  if (isstart.value === true) {
    lushuPoint.value.start.lng = e.point.lng
    lushuPoint.value.start.lat = e.point.lat
  }
  else if (isstart.value === false) {
    lushuPoint.value.end.lng = e.point.lng
    lushuPoint.value.end.lat = e.point.lat
  }
  else {
    ElMessage.error('无法设置，请先点击设置起始位置或设置终点位置按钮')
  }
}
</script>



<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>道路地图查询</span>
        <div class="extra">

          <span> 城镇道路：</span>
          <el-select placeholder="请选择" v-model="lushuPoint.pathName" clearable>
            <el-option v-for="c in paths" :key="c" :label="c" :value="c">
            </el-option>
          </el-select>

          <el-button style="margin-left: 20px;" type="primary" @click="toget">查询</el-button>
          <el-button v-if="userInfoStore.userinfo.userName === 'comum'" style="margin-left: 20px;" type="primary"
            @click="tools">{{ buttonname }}</el-button>
        </div>

      </div>
    </template>
    <!-- 搜索表单 -->
    <el-form inline>
      <el-row style="margin-bottom: 10px;" v-if="istools">
        <el-col :span="12">
          <div>
            <span>起始位置经度：{{ lushuPoint.start.lng }} </span>
            <span style="margin-left: 10px;">起始位置纬度：{{ lushuPoint.start.lat }} </span>
            <el-button style="margin-left: 20px;" type="primary" @click="setstart">设置起始位置</el-button>
            <el-button style="margin-left: 20px;" type="primary" @click="comfir1">确定</el-button>

          </div>
        </el-col>
        <el-col :span="12">
          <span>终点位置经度：{{ lushuPoint.end.lng }}</span>
          <span style="margin-left: 10px;">终点位置纬度：{{ lushuPoint.end.lat }}</span>
          <el-button style="margin-left: 20px;" type="primary" @click="setend">设置终点位置</el-button>
          <el-button style="margin-left: 20px;" type="primary" @click="comfir2">确定</el-button>

        </el-col>
      </el-row>

      <el-row>
        <el-col :span="24">
          <baidu-map class="bm-view" :zoom="10" :center="entity" :scroll-wheel-zoom="true"
            @rightclick="getCurrentPoint">
            <bm-driving v-if="f" :start="lushuPoint.start" :end="lushuPoint.end" @searchcomplete="handleSearchComplete"
              :panel="false" :autoViewport="true"></bm-driving>

          </baidu-map>
        </el-col>
      </el-row>
    </el-form>

  </el-card>


</template>

<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}

.bm-view {
  width: 100%;
  height: 600px;
}

.point {
  position: absolute;
  width: 10px;
  height: 10px;
  background-color: red;
  border-radius: 50%;
}

.extra {
  display: flex;
  justify-content: flex-end;
  align-items: center;
}
</style>